(function(_scope){
	_scope.storage = pi.base.extend({
		"$Init":function(_name){
			this.environment.setName(_name);
			this.environment.setStorageType(
				window.globalStorage&&!pi.env.ie6&&!pi.env.ie7?1:
				pi.env.ie?2:0
			);
		
			switch(this.environment.getStorageType()){
				case 0:
					throw Error("Your browser doesn't support dom storage.");
				case 1:
					this.environment.setDomain(document.domain);
					break;
				case 2:
					this.environment.setElement(
						new pi.element("SPAN").environment.addStyle({ "display":"none", "behavior":"url(#default#userdata)" }).insert(document.body)
					);
					
					this.environment.getElement().element.load("pi_storage");
					break;
			};
			return this;
		},
		"get":function(){
			return this.environment.getValue.apply(this.environment,arguments);
		},
		"isStored":function(){
			return this.environment.getIsStored.apply(this.environment,arguments);
		},
		"remove":function(){
			this.environment.removeValue.apply(this.environment,arguments);
			return this;
		},
		"set":function(){
			this.environment.setValue.apply(this.environment,arguments);
			return this;
		},
		"environment":{
			"_domain":null, "_element":null, "_isStored":false, "_name":"", "_storageType":0, "_value":"",
			"getIsStored":function(_callback){
				switch(this.getStorageType()){
					case 1:
						return Boolean( window.globalStorage[ this.getDomain() ][ this.getName() ] );
						break;
					case 2:
						return Boolean( this.getElement().attribute.get( this.getName() ) );
						break;
				}
			},
			"getValue":function(_callback){
				
				if(!this.getIsStored()){
					return null;
				}
			
				switch(this.getStorageType()){
					case 1:
						return window.globalStorage[ this.getDomain() ][ this.getName() ].value;
						break;
					case 2:
						
						return this.getElement().attribute.get( this.getName() );
						break;
				}
			},
			"removeValue":function(){
				switch( this.getStorageType() ){
					case 1:
						delete window.globalStorage[ this.getDomain() ][this.getName()];
						break;
					case 2:
						this.getElement().attribute.remove(this.getName());
						this.getElement().environment.getElement().save("pi_storage");
						break;
				};
				return this._parent_;
			},
			"setValue":function(_value){
				switch( this.getStorageType() ){
					case 1:
						window.globalStorage[ this.getDomain() ][this.getName()] = _value;
						break;
					case 2:
						this.getElement().attribute.set(this.getName(),_value);
						this.getElement().environment.getElement().save("pi_storage");
						break;
				};
				this._setValue(_value);
				return this._parent_;
			}
		}
	});
})(pi);